package com.hongyi.demo.controller;

import com.hongyi.common.framework.domain.JsonResult;
import com.hongyi.common.mongoDB.service.MongoDBService;
import com.hongyi.common.mongoDB.utils.MongoDbUtils;
import com.hongyi.common.mongoDB.utils.PageHelper;
import com.hongyi.demo.domain.mongo.LycheeMongo;
import org.springframework.data.domain.Sort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * mongoDB通用测试控制器
 *
 * @author DingHao
 * @date 2025/7/5
 **/
@RestController
@RequestMapping("/demo/mongoDB")
public class MongoController {

    @Autowired
    private MongoDBService mongoDBService;

    /**
     * 创建mogoDB集合
     *
     * @param name 集合名称
     */
    @GetMapping(value = "/create")
    public JsonResult<Void> list(String name) {
        mongoDBService.createCollection(name);
        return JsonResult.ok() ;
    }

    /**
     * 删除mogoDB集合
     *
     * @param name 集合名称
     */
    @DeleteMapping(value = "/drop")
    public JsonResult<Void> drop(String name) {
        mongoDBService.dropCollection(name);
        return JsonResult.ok() ;
    }

    /**
     * 向集合中添加文档
     *
     * @param lycheeMongo 数据
     */
    @PostMapping(value = "/add")
    public JsonResult<Void> add(@RequestBody LycheeMongo lycheeMongo) {
        mongoDBService.insert(lycheeMongo, "lychee_demo");
        return JsonResult.ok() ;
    }

    /**
     * 删除文档数据
     *
     * @param id 主键
     */
    @DeleteMapping(value = "/delete")
    public JsonResult<Void> delete(@RequestParam("id") String id) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        Criteria criteria = MongoDbUtils.getCriteria(param, null);
        mongoDBService.delete(criteria, LycheeMongo.class);
        return JsonResult.ok() ;
    }

    /**
     * 修改文档数据
     *
     * @param lycheeMongo 数据
     */
    @PutMapping(value = "/edit")
    public JsonResult<Void> edit(@RequestBody LycheeMongo lycheeMongo) {
        // 构造 Criteria 查询条件：仅根据 _id 匹配
        Map<String, Object> criteriaMap = new HashMap<>();
        criteriaMap.put("_id", lycheeMongo.getId());
        Criteria criteria = MongoDbUtils.getCriteria(criteriaMap);
        // 构造 Update 对象
        Update update = MongoDbUtils.buildUpdate(lycheeMongo, LycheeMongo.class);
        mongoDBService.updateOne(criteria, update, LycheeMongo.class);
        return JsonResult.ok() ;
    }

    /**
     * 查询集合中所有数据
     *
     * return 集合中所有数据
     */
    @GetMapping(value = "/list")
    public JsonResult<List<LycheeMongo>> list() {
        List<LycheeMongo> lycheeDemo = (List<LycheeMongo>)mongoDBService.findAll(LycheeMongo.class, "lychee_demo");
        return JsonResult.ok(lycheeDemo) ;
    }

    /**
     * 分页查询集合中指定数据
     *
     * @param param  查询条件
     * return 分页数据
     */
    @GetMapping(value = "/pageList")
    public PageHelper<LycheeMongo> pageList(@RequestParam Map<String, Object> param) {
        //条件查询过滤掉pageNum和pageSize字段
        List<String> skipFields = new ArrayList<>();
        skipFields.add("pageNum");
        skipFields.add("pageSize");
        Criteria criteria = MongoDbUtils.getCriteria(param, skipFields);
        //设置排序字段
        List<Sort> sortList = new ArrayList<>();
        sortList.add(Sort.by(Sort.Order.desc("birthDay")));
        //执行查询
        PageHelper<LycheeMongo> pageData = (PageHelper<LycheeMongo>) mongoDBService.findByCondition(criteria,
                LycheeMongo.class,
                Integer.parseInt((String) param.get("pageNum")),
                Integer.parseInt((String) param.get("pageSize")), sortList);
        return pageData;
    }

    /**
     * 查询文档详情
     *
     * @param id 主键
     * return 详情数据
     */
    @GetMapping(value = "/getDocumentById")
    public JsonResult<LycheeMongo> getDocumentById(@RequestParam("id") String id) {
        LycheeMongo LycheeMongo = (LycheeMongo) mongoDBService.findById(id, LycheeMongo.class);
        return JsonResult.ok(LycheeMongo) ;
    }

}
